package com.guguskins.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guguskins.back.entity.GuGoods;
import com.guguskins.back.entity.bo.ContractGoodsBo;
import com.guguskins.back.entity.vo.PageVo;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 饰品表 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-05-14
 */
@RestController
@RequestMapping("/goods")
public class GuGoodsController {

    @Autowired
    private GuGoodsService goodsService;

    @GetMapping("/back/page")
    public ResultVo backPage(@RequestParam(value = "p", required = false, defaultValue = "1") int p,
                             @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                             @RequestParam(value = "searchKey", required = false, defaultValue = "") String searchKey) {
        PageVo<GuGoods> result = goodsService.backPage(p, size, searchKey);
        return ResultVo.success(result);
    }

    @GetMapping("/exteriors")
    public ResultVo exteriors() {
        List<String> exteriors = goodsService.listObjs(new LambdaQueryWrapper<GuGoods>() {
            {
                select(GuGoods::getGoodsTagExterior);
                groupBy(GuGoods::getGoodsTagExterior);
            }
        }, Object::toString);
        return ResultVo.success(exteriors);
    }

    @PostMapping("/back/changePrice")
    public ResultVo changePrice(@RequestBody GuGoods goods) {
        if (goodsService.changePrice(goods)) {
            return ResultVo.success("修改成功");
        }
        return ResultVo.fail("修改失败");
    }

    @GetMapping("/back/contract/goodsPage")
    public ResultVo backContractGoodsPage(@RequestParam(value = "p", required = false, defaultValue = "1") int p,
                                          @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                                          @RequestParam(value = "searchKey", required = false, defaultValue = "") String searchKey) {
        PageVo<GuGoods> goodsList = goodsService.backContractGoodsPage(p, size, searchKey);
        return ResultVo.success(goodsList);
    }

    @PostMapping("/back/contract/add")
    public ResultVo backContractAdd(@RequestBody ContractGoodsBo contractGoodsBo) {
        if (goodsService.backContractAdd(contractGoodsBo)) {
            return ResultVo.success("添加成功");
        }
        return ResultVo.fail("添加失败");
    }

    @PostMapping("/back/contract/changeState")
    public ResultVo backContractChangeState(@RequestBody GuGoods goods) {
        if (goodsService.backContractChangeState(goods)) {
            return ResultVo.success("修改成功");
        }
        return ResultVo.fail("修改失败");
    }

    @GetMapping("/back/list")
    public ResultVo backList(@RequestParam String searchKey) {

        return ResultVo.success(goodsService.list(new LambdaQueryWrapper<GuGoods>() {
            {
                like(GuGoods::getGoodsName, searchKey);
            }
        }));
    }
    @PostMapping("/back/findGoods")
    public ResultVo findBox(@RequestParam(value = "goodsId",required = false,defaultValue = "1") int goodsId){
        return ResultVo.success(goodsService.getById(goodsId));
    }
}
